﻿using HMACAuthentication.WebApi.Filters;
using YMJF_Service.Common;
using YMJF_Service.Helpers;
using YMJF_Service.Models;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace YMJF_Service.Controllers
{
    /// <summary>
    /// 招聘管理
    /// </summary>
    [RoutePrefix("api/advertisefor")]
    public class AdvertiseForController : ApiController
    {
        ConnectionStringMySQLDB db = new ConnectionStringMySQLDB();


        /// <summary>
        /// 获取招聘列表(前端)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("list2")]
        public ApiResult GetList2()
        {
            ApiResult res = new ApiResult();
            try
            {
                List<AdvertiseFor> list = db.Fetch<AdvertiseFor>("where SysDeleted=0 order by SysOrder");
                res.code = HttpCode.success;
                res.message = "获取列表成功";
                res.data = list;
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "AdvertiseFor", " 获取招聘列表(前端)错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 获取招聘列表(后台)
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="pagesize">每页行数</param>  
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        [HMACAuthenticationAttribute]
        public ApiResult GetList(long page, long pagesize)
        {
            ApiResult res = new ApiResult();
            try
            {
                Page<AdvertiseFor> list = db.Page<AdvertiseFor>(page, pagesize, "where SysDeleted=0 order by SysOrder");
                res.code = HttpCode.success;
                res.message = "获取列表成功";
                res.data = list;
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "AdvertiseFor", "获取招聘列表(后台)错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }


        /// <summary>
        /// 获取招聘信息
        /// </summary>
        /// <param name="ID">招聘ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("get")]
        [HMACAuthenticationAttribute]
        public ApiResult GetAccessory(string ID)
        {
            ApiResult res = new ApiResult();
            try
            {
                AdvertiseFor data = AdvertiseFor.SingleOrDefault(" where SysDeleted = 0 and ID = @0 ", ID);
                if (data != null )
                {
                    res.code = HttpCode.success;
                    res.message = "查询成功";
                    res.data = data;
                }
                else
                {
                    res.code = HttpCode.Que_Failed;
                    res.message = "查询失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "AdvertiseFor", "获取招聘信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }



        /// <summary>
        /// 添加招聘信息
        /// </summary>
        /// <param name="data">招聘信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("add")]
        [HMACAuthenticationAttribute]
        public ApiResult AddAdvertiseFor(AdvertiseFor data)
        {
            ApiResult res = new ApiResult();

            try
            {
                string user = "admin";
                data.ID = Guid.NewGuid().ToString();
                data.SysCreate = DateTime.Now;
                data.SysCreatedBy = user;
                data.SysLastUpd = DateTime.Now;
                data.SysLastUpdBy = user;
                data.SysDeleted = false;
                object obj = data.Insert();
                if (obj != null)
                {
                    res.code = HttpCode.success;
                    res.message = "添加成功";
                    res.data = data;
                }
                else
                {
                    res.code = HttpCode.Add_Failed;
                    res.message = "添加失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "AdvertiseFor", "添加招聘信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }

        /// <summary>
        /// 修改招聘信息
        /// </summary>
        /// <param name="data">招聘信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("update")]
        [HMACAuthenticationAttribute]
        public ApiResult UpdateAccessory(AdvertiseFor data)
        {
            ApiResult res = new ApiResult();

            try
            {
                AdvertiseFor data2 = AdvertiseFor.SingleOrDefault(" where ID = @0 ", data.ID);
                data2.Job = data.Job;
                data2.Mode = data.Mode;
                data2.Experience = data.Experience;
                data2.Education = data.Education;
                data2.Content = data.Content;
                data2.WorkPlace = data.WorkPlace;
                data2.SysOrder = data.SysOrder;
                data2.SysLastUpd = DateTime.Now;
                int obj = data2.Update();
                if (obj > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "修改成功";
                    res.data = data2;
                }
                else
                {
                    res.code = HttpCode.Mod_Failed;
                    res.message = "修改失败";
                }

            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "AdvertiseFor", "修改招聘信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }


        /// <summary>
        ///  删除招聘信息
        /// </summary>
        /// <param name="ID">招聘信息ID</param>
        /// <returns></returns>
        [HttpPost]
        [Route("del")]
        [HMACAuthenticationAttribute]
        public ApiResult DelAdvertiseFor(string ID)
        {
            ApiResult res = new ApiResult();

            try
            {
                int obj = AdvertiseFor.Update(string.Format(" set SysDeleted = 1,SysLastUpd = '{1}' where ID = '{0}' ", ID, DateTime.Now));
                if (obj > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "删除成功";
                }
                else
                {
                    res.code = HttpCode.Del_Failed;
                    res.message = "删除失败";
                }

            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL(MessageType.Exception, "accessory", "删除招聘信息错误：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;

        }


    }
}
